<template>
  <div class="side-info-section">
    <!-- 视频信息卡片 - 紧凑样式 -->
    <div class="video-info-card">
      <h3>视频信息</h3>
      <div class="info-content">
        <div class="info-item">
          <label>URL:</label>
          <span>{{ recording?.recording_url ? (recording.recording_url.length > 20 ? recording.recording_url.substring(0, 20) + '...' : recording.recording_url) : '-' }}</span>
        </div>
        <div class="info-item">
          <label>分辨率:</label>
          <span>{{ recording?.resolution || '-' }}</span>
        </div>
        <div class="info-item">
          <label>码率:</label>
          <span>{{ recording?.bitrate || '-' }}</span>
        </div>
        <div class="info-item">
          <label>时长:</label>
          <span>{{ formatTime(recording?.duration || 0) }}</span>
        </div>
        <div class="info-item">
          <label>大小:</label>
          <span>{{ recording ? formatFileSize(recording.size) : '-' }}</span>
        </div>
      </div>
    </div> 
    
    <!-- 拆条记录列表 - 自适应剩余空间 -->
    <div class="clipping-records">
      <div class="records-header">
        <h3>拆条记录</h3>
      </div>
      <div class="records-list">
        <div v-if="clippingRecords.length === 0" class="empty-records">
          <div class="empty-text">暂无拆条记录</div>
        </div>
        <el-table 
          v-else 
          :data="clippingRecords" 
          size="small"
          class="records-table"
        >
          <el-table-column prop="name" label="名称" min-width="120" show-overflow-tooltip />
          <el-table-column prop="startTime" label="开始" width="100" />
          <el-table-column prop="endTime" label="结束" width="100" />
          <el-table-column prop="duration" label="时长" width="60" />
          <el-table-column label="操作" width="120" fixed="right">
            <template #default="scope">
              <div class="clipping-item-actions">
                <el-button 
                  size="small" 
                  type="primary" 
                  @click="handlePreview(scope.row)"
                  title="预览"
                  :icon="VideoPlay"
                />
                <el-button 
                  size="small" 
                  @click="handleEdit(scope.row)"
                  title="编辑"
                  :icon="Edit"
                />
                <el-button 
                  size="small" 
                  type="danger" 
                  @click="handleDelete(scope.row.id)"
                  title="删除"
                  :icon="Delete"
                />
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineProps, defineEmits } from 'vue'
import { VideoPlay, Edit, Delete } from '@element-plus/icons-vue'

const props = defineProps({
  recording: {
    type: Object,
    default: null
  },
  clippingRecords: {
    type: Array,
    default: () => []
  }
})

const emit = defineEmits(['previewClip', 'editRecord', 'deleteRecord'])

const formatTime = (seconds) => {
  if (!seconds || isNaN(seconds)) return '00:00:00'
  const h = Math.floor(seconds / 3600)
  const m = Math.floor((seconds % 3600) / 60)
  const s = Math.floor(seconds % 60)
  return `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`
}

const formatFileSize = (bytes) => {
  if (!bytes || bytes === 0) return '0 B'
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}

const handlePreview = (row) => {
  emit('previewClip', row)
}

const handleEdit = (row) => {
  emit('editRecord', row)
}

const handleDelete = (id) => {
  emit('deleteRecord', id)
}
</script>

<style scoped>
.side-info-section {
  width: 320px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  box-sizing: border-box;
}

.video-info-card {
  background-color: var(--bg-color);
  border-radius: var(--border-radius-base);
  padding: 12px;
  border-left: 4px solid var(--success-color);
}

.video-info-card h3 {
  margin: 0 0 10px 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-color);
}

.info-content {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
}

.info-item label {
  color: var(--text-secondary-color);
  font-weight: 500;
  min-width: 50px;
}

.info-item span {
  color: var(--text-color);
  font-family: 'Consolas', 'Monaco', monospace;
  word-break: break-all;
}

.clipping-records {
  flex: 1;
  background-color: var(--bg-color);
  border-radius: var(--border-radius-base);
  display: flex;
  flex-direction: column;
  border-left: 4px solid var(--warning-color);
  min-height: 0;
}

.records-header {
  padding: 8px 10px;
  background-color: var(--bg-color);
  border-bottom: 1px solid var(--border-light-color);
  font-weight: 600;
  color: var(--text-color);
  font-size: 12px;
  flex-shrink: 0;
  margin: 0;
}

.records-list {
  flex: 1;
  padding: 8px;
  overflow-y: hidden;
  min-height: 0;
}

.empty-records {
  text-align: center;
  color: var(--text-placeholder-color);
  padding: 30px 16px;
  font-style: italic;
  font-size: 13px;
}

.records-table {
  border-radius: var(--border-radius-small);
  overflow: hidden;
  width: 100%;
}

.records-table :deep(.el-table) {
  max-height: none !important;
}

.records-table :deep(.el-table__body-wrapper) {
  max-height: none;
}

.records-table :deep(.el-table__header-wrapper) {
  background-color: var(--bg-color);
}

.records-table :deep(.el-table__header th) {
  background-color: var(--bg-color) !important;
  border-bottom: 1px solid var(--border-light-color);
}

.records-table :deep(.el-table__body tr:hover) > td {
  background-color: rgba(24, 144, 255, 0.05) !important;
}

.records-table :deep(.el-table__body tr.el-table__row--striped td) {
  background-color: var(--bg-color);
}

.clipping-item-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: center;
}

.clipping-item-actions .el-button {
  padding: 4px 12px;
  font-size: 12px;
  height: 28px;
  line-height: 28px;
}

.clipping-item-actions .el-button--primary {
  background-color: var(--primary-color);
  border-color: var(--primary-color);
}

.clipping-item-actions .el-button--danger {
  background-color: var(--error-color);
  border-color: var(--error-color);
}

.clipping-item-actions .el-button--primary:hover {
  background-color: #409eff;
  border-color: #409eff;
}

.clipping-item-actions .el-button--danger:hover {
  background-color: #f56c6c;
  border-color: #f56c6c;
}

@media (max-width: 1200px) {
  .side-info-section {
    width: 280px;
  }
  
  .info-content {
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  }
}

@media (max-width: 992px) {
  .side-info-section {
    width: 100%;
    height: 200px;
  }
}

@media (max-width: 768px) {
  .info-content {
    grid-template-columns: 1fr;
    gap: 8px;
  }
  
  .clipping-records {
    height: auto;
    min-height: 200px;
  }
}
</style>